

//Created by Jeremy Legendre on 11/12/08///
//email me: jeremy@pointlessprogrammers.com
//////////////////////////////////////////////////////////////////////////////
// The MIT License
// 
// Copyright (c) 2008-2009 Jeremy Legendre
// 
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions: The above copyright notice and this permission
// notice shall be included in all copies or substantial portions of the
// Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
// KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//////////////////////////////////////////////////////////////////////////////

#import "AeroWindow.h"
#import <ApplicationServices/ApplicationServices.h>

@implementation AeroWindow

-(id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)aStyle
				 backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag
{	
    if(self = [super initWithContentRect:contentRect
							   styleMask:aStyle backing:NSBackingStoreBuffered defer:NO])
    {
		[self enableBlurForWindow:self];
		[self setOpaque:NO];
		[self setBackgroundColor:[NSColor clearColor]];
		[self setAlphaValue:1.0];
		[self setBackgroundColor:[NSColor colorWithPatternImage:[NSImage
																imageNamed:@"AeroLook.psd"]]];
		
    }
	
	return self;
}

typedef void * CGSConnectionID;

extern OSStatus CGSNewConnection(const void **attr, CGSConnectionID *id);

- (void)enableBlurForWindow:(NSWindow *)window
{
	
	CGSConnectionID _myConnection;
	uint32_t __compositingFilter;
	
	int __compositingType = 1; // Apply filter to contents underneath the window, then draw window normally on top
	
	/* Make a new connection to CoreGraphics, alternatively you could use the main connection*/
	
	CGSNewConnection(NULL , &_myConnection);
	
	/* The following creates a new CoreImage filter, then sets its options with a dictionary of values*/
	
	CGSNewCIFilterByName (_myConnection, (CFStringRef)@"CIGaussianBlur", &__compositingFilter);
	NSDictionary *optionsDict = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:3.0] forKey:@"inputRadius"];
	CGSSetCIFilterValuesFromDictionary(_myConnection, __compositingFilter, (CFDictionaryRef)optionsDict);
	
	/* Now just switch on the filter for the window */
	
	CGSAddWindowFilter(_myConnection, [window windowNumber], __compositingFilter, __compositingType );
}
@end


